package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.constant.MqConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * order微服务超时取消订单的延迟消息队列配置类
 */
@Configuration
public class OrderDelayMqConfig {

    //交换机
    @Bean
    public Exchange orderExchange(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(MqConst.EXCHANGE_DIRECT_ORDER_CANCEL,"x-delayed-message",
                true,false,arguments);
    }

    //队列
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_ORDER_CANCEL).build();
    }

    //将交换机和队列进行绑定
    @Bean
    public Binding bindingOrderExchangeAndOrderQueue(
            @Qualifier(value = "orderExchange") Exchange orderExchange,
            @Qualifier(value = "orderQueue") Queue orderQueue
    ){
        return BindingBuilder.bind(orderQueue).to(orderExchange).with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
}
